//
//  WXSWeekDetailViewController.m
//  SleepBooks
//
//  Created by Ivan_Yang on 2016/9/21.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "WXSWeekDetailViewController.h"

@interface WXSWeekDetailViewController ()
{
    UIScrollView *scrollView;
}
@property (nonatomic, strong) UIView * weekChooseView;//周选择view
@property (nonatomic, strong) UIView * chartView;//图表view
@property (nonatomic, strong) UIView * showColorView;//颜色提示view
@property (nonatomic, strong) UIView * reportView;//周报告view
@property (nonatomic, strong) UIButton * leftbutton;//左
@property (nonatomic, strong) UIButton * rightbutton;//右
@property (nonatomic, strong) UILabel * weekLabel;//周日期
@property (nonatomic, strong) UILabel * weekReportLabel;//周报告label

@property (nonatomic, strong) NSDate * currentDate;//记录选择后的时间

- (void)_initializeAppearance;//初始化
- (void)_makeConstraints;//约束

@end

@implementation WXSWeekDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initializeAppearance];
}

#pragma mark - private method

- (void)_initializeAppearance
{
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49 - 30)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight);
    [self.view addSubview:scrollView];
    [self.view addSubview:self.weekChooseView];
    [self.view addSubview:self.chartView];
    [self.view addSubview:self.showColorView];
    [self.view addSubview:self.reportView];
    [self.weekChooseView addSubview:self.leftbutton];
    [self.weekChooseView addSubview:self.rightbutton];
    [self.weekChooseView addSubview:self.weekLabel];
    [self.reportView addSubview:self.weekReportLabel];
    [self _makeConstraints];
}

- (void)_makeConstraints
{
    [_weekChooseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    [_chartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_weekChooseView.mas_bottom);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(_showColorView.mas_top);
    }];
    [_showColorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(_reportView.mas_top);
    }];
    [_reportView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(150);
    }];
    [_leftbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.mas_equalTo(_weekChooseView.mas_centerY);
    }];
    [_rightbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.mas_equalTo(_weekChooseView.mas_centerY);
    }];
    [_weekLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(21);
        make.center.mas_equalTo(_weekChooseView.center);
    }];
    [_weekReportLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(21);
        make.center.mas_equalTo(_reportView.center);
    }];
}

- (NSString *)getMonthBeginAndEnd:(NSDate *)currentDate
{
    if (currentDate == nil)
    {
        currentDate = [NSDate date];
    }
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];
    NSDate *begin = nil;
    NSDate *end = nil;
    double intervel = 0;
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitWeekOfMonth startDate:&begin interval:&intervel forDate:currentDate];
    if (ok)
    {
        end = [begin dateByAddingTimeInterval:intervel - 1];
    }
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *beginString = [myDateFormatter stringFromDate:begin];
    NSString *endString = [myDateFormatter stringFromDate:end];
    
    NSString *beginAndEnd = [NSString stringWithFormat:@"%@ - %@",beginString,endString];
    return beginAndEnd;
}

#pragma mark - action

- (void)leftButtonTapped:(UIButton *)sender
{
    NSDate *date =[NSDate date];
    NSDate *preDate = [_currentDate ? _currentDate : date dateByAddingTimeInterval:-60*60*24*7];
    self.weekLabel.text =  [self getMonthBeginAndEnd:preDate];
    _currentDate = preDate;
}

- (void)rightButtonTapped:(UIButton *)sender
{
    NSDate *date =[NSDate date];
    NSDate *nextDate = [_currentDate ? _currentDate : date dateByAddingTimeInterval:60*60*24*7];
    self.weekLabel.text =  [self getMonthBeginAndEnd:nextDate];
    _currentDate = nextDate;
}

#pragma mark - lazy load

- (UIView *)weekChooseView
{
    if (_weekChooseView == nil)
    {
        _weekChooseView = [[UIView alloc]init];
    }
    return _weekChooseView;
}

- (UIView *)chartView
{
    if (_chartView == nil)
    {
        _chartView = [[UIView alloc]init];
        _chartView.backgroundColor = [UIColor cyanColor];
    }
    return _chartView;
}

- (UIView *)showColorView
{
    if (_showColorView == nil)
    {
        _showColorView = [[UIView alloc]init];
        _showColorView.backgroundColor = [UIColor blueColor];
    }
    return _showColorView;
}

- (UIView *)reportView
{
    if (_reportView == nil)
    {
        _reportView = [[UIView alloc]init];
        _reportView.backgroundColor = [UIColor yellowColor];
    }
    return _reportView;
}

- (UIButton *)leftbutton
{
    if (_leftbutton == nil)
    {
        _leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftbutton addTarget:self action:@selector(leftButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_leftbutton setImage:[UIImage imageNamed:@"左"] forState:UIControlStateNormal];
    }
    return _leftbutton;
}

- (UIButton *)rightbutton
{
    if (_rightbutton == nil)
    {
        _rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightbutton addTarget:self action:@selector(rightButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_rightbutton setImage:[UIImage imageNamed:@"右"] forState:UIControlStateNormal];
    }
    return _rightbutton;
}

- (UILabel *)weekLabel
{
    if (_weekLabel == nil)
    {
        _weekLabel = [[UILabel alloc]init];
        _weekLabel.font = [UIFont systemFontOfSize:12];
        _weekLabel.textAlignment = NSTextAlignmentCenter;
        _weekLabel.textColor = [UIColor whiteColor];
        _weekLabel.text = [self getMonthBeginAndEnd:nil];
    }
    return _weekLabel;
}

- (UILabel *)weekReportLabel
{
    if (_weekReportLabel == nil)
    {
        _weekReportLabel = [[UILabel alloc]init];
        _weekReportLabel.text = @"周分析报告";
        _weekReportLabel.textColor = [UIColor whiteColor];
        _weekReportLabel.textAlignment = NSTextAlignmentCenter;
        _weekReportLabel.font = [UIFont systemFontOfSize:18];
    }
    return _weekReportLabel;
}

@end
